package cn.kingyang.yiyi.mvp.presenter;
/////////////////////////////////////////////////////////////////
//                          _ooOoo_                            //
//                         o8888888o                           //
//                         88" . "88                           //
//                         (| ^_^ |)                           //
//                         O\  =  /O                           //
//                      ____/`---'\____                        //
//                    .'  \\|     |//  `.                      //
//                   /  \\|||  :  |||//  \                     //
//                  /  _||||| -:- |||||-  \                    //
//                  |   | \\\  -  /// |   |                    //
//                  | \_|  ''\---/''  |   |                    //
//                  \  .-\__  `-`  ___/-. /                    //
//                ___`. .'  /--.--\  `. . ___                  //
//              ."" '<  `.___\_<|>_/___.'  >'"".               //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |              //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /              //
//      ========`-.____`-.___\_____/___.-`____.-'========      //
//                           `=---='                           //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      //
//    Code is far away from bug with the buddha protecting!    //
/////////////////////////////////////////////////////////////////

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

import cn.kingyang.yiyi.mvp.view.BaseView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by KingYang on 2017/2/26.
 * E-Mail: admin@kingyang.cn
 */
public abstract class BasePresenter<V extends BaseView> {
    private Reference<V> mViewRef;//使用弱引用，防止内存泄漏
    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    protected V getView() {
        return mViewRef != null ? mViewRef.get() : null;
    }

    public void attachView(V view) {
        mViewRef = new WeakReference<>(view);
    }

    public boolean isAttached() {
        return mViewRef != null && mViewRef.get() != null;
    }

    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
        compositeDisposable.clear();
    }

    void addDisposable(Flowable flowable, Consumer consumer) {
        compositeDisposable.add(flowable.
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().onError(throwable);
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        getView().hideProgress();
                    }
                })
                .subscribe(consumer));
    }

}
